<?php
@session_start();
include_once("../initial.php");
$DOCUMENT_ROOT=$_SERVER["DOCUMENT_ROOT"];
include_once(constant('PROJECT_DIR')."/logic/courseLogic.php");
include_once(constant('PROJECT_DIR')."/logic/userLogic.php");
include_once(constant('PROJECT_DIR')."/logic/deviceLogic.php");
$LIST_NUMBER_PER_PAGE=10;
require_once(constant('PROJECT_DIR')."/checkLogin.php");
require_once(constant('PROJECT_DIR')."/checkAdminLogin.php");
$adminId=$_SESSION['user'];

//logic
$action="";
if(isset($_POST['action']))
{
     $action = $_POST['action'];
}
else if(isset($_GET['action']))
{
     $action=$_GET['action'];
}
//write course
$courselg = new courseLogic();
$errorInfo="";
$courseSearchResult=array();
$courseSearchNote="";
switch($action)
{
     case "addcourse":
         $courseInfo=array();
         $courseInfo['name']=$_POST['name'];
         $courseInfo['teacherid']=$_POST['teacherid'];
         $courseInfo['monitor']=$_POST['monitorid'];
         $courseInfo['booktime']=$_POST['booktime'];
         $courseInfo['begindate']=$_POST['begintime'];
         $courseInfo['enddate']=$_POST['endtime'];
         if($courselg->get_course_num(array("name"=>$_POST['name']))>0)
         {
             $errorInfo="插入失败：".$courseInfo['name']."已经存在。";
             break;
         }
         else
         {
             $courselg->insert_course($courseInfo);
         }
         break;
    case "deletecourse":
        $delDevices=$_POST['courses'];
        foreach($delDevices as $did)
        {
            $courselg->delete_course($did);
        }
        break;
    case "modifycourse":
    		$courseId=$_POST['courseid'];
    		$name=$_POST['name'];
    		$teacherid=$_POST['teacherid'];
    		$monitor=$_POST['monitor'];
    		$begindate=$_POST['begindate'];
    		$enddate=$_POST['enddate'];
    		$info=$_POST['info'];
    		$array=array("name"=>$name, "teacherid"=>$teacherid,"monitor"=>$monitor, "begindate"=>$begindate, "enddate"=>$enddate,"info"=>$info);
    		$courselg->update_course($array, $courseId);
    		break;
    case "removelistitem":
    		$listitem=$_GET['list'];
    		if($listitem == "user")
    		{
		    		$courseid=$_GET['id'];
		    		$userid=$_GET['uid'];
		    		$courselg->delete_course_user($courseid, $userid);
    		}
    		else if($listitem == "device")
    		{
    				$courseid=$_GET['id'];
    				$deviceid=$_GET['deviceid'];
    				$courselg->delete_course_device($courseid, $deviceid);
    		}
    		else
    		{}
    		break;
		case "searchCourse":
				$keyword = $_GET['keyword'];
    		if(strlen($keyword)<3)
    		{
    				$courseSearchNote .="输入的关键词太短，不进行搜索,请输入长度超过3的字符串.";
    				$smarty->assign("searchNote", $courseSearchNote);
    		}
    		//keyword长度要超过2
    		$courseSearchResult = $courselg->searchCourse($keyword, "limit 0,30");//默认显示
    		if(count($courseSearchResult)==30)
    		{
    				$courseSearchNote .="搜索词得到太多结果，只显示30条，请输入更长的搜索词";
    				$smarty->assign("searchNote", $courseSearchNote);
    		}
    		if(count($courseSearchResult)==0)
    		{
    				$courseSearchNote .="没有结果，展示所有用户";
    				$smarty->assign("searchNote", $courseSearchNote);
    		}
    		
    		$smarty->assign("keyword", $keyword);
    		break;
}
//read courses info
if(isset($_GET['id']))
{
		$editCourse=$courselg->get_course(array("id"=>$_GET['id']));
		$smarty->assign("editCourse", $editCourse[0]);
		
		//如果是列出相关用户或者设备
		$listItem="";
		if(isset($_GET['list']))
		{
				$listItem=$_GET['list'];
		}
		if($listItem == "user")
		{
				//用户要求列出与课程相关的用户
				$courseUsers=$courselg->get_course_users($editCourse[0]['id']);//只能获得id，还需要扩充其他信息
				$userlg=new userLogic();
				for($i=0; $i<count($courseUsers); $i++)
				{
					$userTmp=$userlg->get_user(array('id'=>$courseUsers[$i]['uid']));
					$courseUsers[$i]['name']=$userTmp[0]['name'];
				}
				$smarty->assign("listItem", "user");
				$smarty->assign("courseUsers", $courseUsers);
		}
		else if($listItem == "device")
		{
				$courseDevices=$courselg->get_course_devices($editCourse[0]['id']);
				$devicelg=new deviceLogic();
				for($i=0; $i<count($courseDevices); $i++)
				{
					$deviceTmp=$devicelg->get_device(array('id'=>$courseDevices[$i]['did']));
					$courseDevices[$i]['name']=$deviceTmp[0]['name'];
					$courseDevices[$i]['barcode']=$deviceTmp[0]['barcode'];
				}
				$smarty->assign("listItem", "device");
				$smarty->assign("courseDevices", $courseDevices);
		}
		else
		{}
}

//handling the page index
$coursesNumber=0;
if(count($courseSearchResult)>0)
{
		$coursesNumber=count($courseSearchResult);
}
else
{
	$coursesNumber=$courselg->get_course_num();
}

$pageIndex=1;
$totalPageNumber=ceil($coursesNumber/$LIST_NUMBER_PER_PAGE);
if(isset($_GET['pageIndex']))
{
     $pageIndex=$_GET['pageIndex'];
     if($pageIndex<1 || $pageIndex > $totalPageNumber)
     {
         $pageIndex=1;
     }
}
$startItem=($pageIndex-1)*$LIST_NUMBER_PER_PAGE;
$ItemNumber=$LIST_NUMBER_PER_PAGE;

$leftPageIndex=$pageIndex<=1?1:$pageIndex-1;
$rightPageIndex=$pageIndex>=$totalPageNumber?$totalPageNumber:$pageIndex+1;
$leftMostPageIndex=$pageIndex<=5?1:$pageIndex-5;//左边显示5个页面索引
$rightMostPageIndex=$totalPageNumber<$pageIndex+5?$totalPageNumber:$pageIndex+5;//右边显示5个页面索引
$smarty->assign("leftMostPageIndex",$leftMostPageIndex);
$smarty->assign("rightMostPageIndex",$rightMostPageIndex);
$smarty->assign("leftPageIndex",$leftPageIndex);
$smarty->assign("rightPageIndex",$rightPageIndex);
$smarty->assign("pageIndex",$pageIndex);
$smarty->assign("coursesNumber", $coursesNumber);
$allcourses=array();
if(count($courseSearchResult)>0)
{
		$allcourses=$courseSearchResult;
}
else
{
		$allcourses=$courselg->get_course('',"order by id desc", "limit $startItem,$ItemNumber");
}
$smarty->assign("courses",$allcourses);
//handling page Index end

if(strlen($errorInfo)>0)
{
     $smarty->assign("insertMessage", $errorInfo);
}
$smarty->assign("projectUrl", constant('PROJECT_URL'));
$smarty->assign("menuSelected", "course");
$smarty->assign("currentUrl","admin/course.php");
$smarty->display("admin/course.html");
?>
